import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import jwt from 'jsonwebtoken'
import moment from 'moment'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: '',
    component: () => import('@/views/Home.vue'),
    children: [
      {
        path: '',
        name: 'index',
        component: () => import('@/views/channel/Index.vue')
      },
      {
        path: '/index/:catalog',
        name: 'catalog',
        component: () => import('@/views/channel/Template1.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    beforeEnter: (to, from, next) => {
      const isLogin = store.state.isLogin
      if (isLogin) {
        next('/center')
      } else {
        next()
      }
    }
  },
  {
    path: '/confirm',
    name: 'confirm',
    component: () => import('@/views/Confirm.vue')
  },
  {
    path: '/reset',
    name: 'reset',
    component: () => import('@/views/Reset.vue')
  },
  {
    path: '/regist',
    name: 'reg',
    component: () => import('@/views/Regist.vue'),
    beforeEnter: (to, from, next) => {
      if (from.name === 'login') {
        next()
      } else {
        next('/login')
      }
    }
  },
  {
    path: '/forget',
    name: 'Forget',
    component: () => import('@/views/Forget.vue')
  },
  {
    path: '/add',
    name: 'Add',
    // a meta field
    meta: { requiresAuth: true },
    component: () => import('@/components/contents/Add.vue')
  },
  {
    path: '/detail/:tid',
    name: 'Detail',
    props: true,
    component: () => import('@/components/contents/Detail.vue'),
    meta: {
      // 通过后台接口动态添加到路由 addRoutes
      types: ['get', 'add', 'delete']
    }
  },

  {
    path: '/user/:uid',
    name: 'home',
    props: true,
    component: () => import('@/views/User.vue')
  },
  {
    path: '/center',
    component: () => import('@/views/Center.vue'),
    linkActiveClass: 'layui-this',
    // a meta field
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'center',
        component: () => import('@/components/user/Center')
      },
      {
        path: 'set',
        // name: 'set',
        linkActiveClass: 'layui-this',
        component: () => import('@/components/user/Settings'),
        children: [
          {
            path: '',
            name: 'info',
            component: () => import('@/components/user/common/MyInfo')
          },
          {
            path: 'pic',
            name: 'pic',
            component: () => import('@/components/user/common/PicUpload')
          },
          {
            path: 'passwd',
            name: 'passwd',
            component: () => import('@/components/user/common/Password')
          },
          {
            path: 'accounts',
            name: 'accounts',
            component: () => import('@/components/user/common/Accounts')
          }
        ]
      },
      {
        path: 'posts',
        // name: 'posts',
        component: () => import('@/components/user/Posts'),
        children: [
          {
            path: '',
            name: 'mypost',
            component: () => import('@/components/user/common/MyPost')
          },
          {
            path: 'mycollection',
            name: 'mycollection',
            component: () => import('@/components/user/common/MyCollection')
          }
        ]
      },
      {
        path: 'msg',
        name: 'msg',
        component: () => import('@/components/user/Msg')
      },
      {
        path: 'others',
        name: 'others',
        component: () => import('@/components/user/Others')
      }

    ]

  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/NotFound')
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  linkExactActiveClass: 'layui-this',
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  const userInfo = JSON.parse(localStorage.getItem('userInfo'))

  if (token !== '' && token !== null) {
    const payload = jwt.decode(token)
    if (moment().isBefore(moment(payload.exp * 1000))) {
      // 一般token 设置 8-24小时 refresh token 设置一个月（可以在请求返回一个token）
      store.commit('setToken', token)
      store.commit('setUserInfo', userInfo)
      store.commit('setIsLogin', true)
    } else {
      localStorage.clear()
    }
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const isLogin = store.state.isLogin
    if (isLogin) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
